<?php
/**
 * @Author: Zhao Yang[873777808@qq.com]
 * @Date:   2015-03-04 18:50:28
 * @Last Modified time: 2015-11-24 00:32:16
 */
// 后台权限控制器
namespace Admin\Controller;
use Admin\Controller\CommonController;
class AuthController extends CommonController{
	private $model;

	public function _initialize()
	{
		parent::_initialize();
		$this->model = D('AdminRelation');
	}

	// 用户列表
	public function index()
	{
		$this->data = $this->model->field('pwd',true)->relation(true)->select();
		$this->display();
	}

	/**
	 * 用户添加
	 * @return [type] [description]
	 */
	public function userAdd()
	{
		if(IS_POST){
			$_POST['code']= enKey();
			$gid = I('post.gid');
			if(!$gid) $this->error('请选择用户组');
			if(!$data = $this->model->create())$this->error($this->model->getError());
			$data['auth_group']= $gid;;
			if($this->model->relation(true)->add($data)){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->group = M('AuthGroup')->select();
			$this->display();
		}
		
	}
	
	

	/**
	 * 禁用用户
	 * @return [type] [description]
	 */
	public function userOff()
	{
		if(IS_POST){
			$id = I('post.id');
			if(M('Admin')->where(array('uid'=>$id))->setField('islock',1)){
				$this->success('禁用成功');
			}
		}else{
			$this->error('页面不存在');
		}
	}
	/**
	 * 解禁
	 * @return [type] [description]
	 */
	public function userOn()
	{
		if(IS_POST){
			$id = I('post.id');
			if(M('Admin')->where(array('uid'=>$id))->setField('islock',0)){
				$this->success('解禁成功');
			}
		}else{
			$this->error('页面不存在');
		}
	}


	// 管理员修改
	public function userEdit()
	{
		if(IS_POST){
			$uid = I('post.uid');
			$gid = I('post.gid');
			if(!$gid) $this->error('请选择用户组');
			if(!$db = $this->model->create()) $this->error($this->model->getError());
			if($gid) $db['auth_group'] = $gid;
			$res = $this->model->relation(true)->save($db);
			if($res>=0){
				$this->success('更新成功');	
			}else{
				$this->error('更新失败');
			}
		}else{
			$uid = I('get.uid',0,'intval');
			$this->group = M('AuthGroup')->select();
			$this->gid = M('AuthGroupAccess')->where(array('uid'=>$uid))->getField('group_id',true);
			$data = $this->model->field('pwd',true)->find($uid);
			$this->assign('data',$data);
			$this->display();
		}
		
	}

	/**
	 * 管理员删除
	 * @return [type] [description]
	 */
	public function userDel()
	{
		if(IS_POST){
			$id = I('post.id',0,'intval');
			$this->model->relation(true)->delete($id);
			$this->success('删除成功');
		}else{
			$this->error('页面不存在');
		}
	}


	/**
	 * 用户组列表
	 * @return [type] [description]
	 */
	public function group()
	{
		$this->data = M('AuthGroup')->select();
		$this->display();
	}

	/**
	 * 用户组编辑表单
	 * @return [type] [description]
	 */
	public function groupEdit(){
		$this->act = '添加';
		$this->data = NULL;
		if(I('get.id',0,'intval')){
			$this->act = '修改';
			$this->data = M('auth_group')->find($id);
		}
		$this->display();
	}

	/**
	 * 用户组编辑表单提交
	 * @return [type] [description]
	 */
	public function ajaxGroup()
	{
		if(IS_POST){
			$model = D('AuthGroup');
			if(!$model->create()) $this->error($model->getError());
			if($model->add($data='',$options=array(),$replace=true)){
				$this->success('操作成功');
			}else{
				$this->error('操作失败');
			}
		}else{
			$this->error('页面不存在');
		}
	}

	/**
	 * 删除用户组
	 */
	public function delGroup()
	{
		if(IS_POST){
			$id = I('post.id',0,'intval');
			if(M('AuthGroup')->delete($id)){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}
	/**
	 * 分配权限
	 * @return [type] [description]
	 */
	public function access()
	{
		$menu = S('menu');
		$data = genTree5($menu);
		$this->assign('data',$data);
		$this->rule = NULL;
		if($gid = I('get.gid',0,'intval')){
			$this->rule = (M('AuthGroup')->where(array('id'=>$gid))->getField('rules'));
		}
		$this->display();
	}


	// 分配权限表单提交
	public function ajaxAccess()
	{
		if(IS_POST){
			$access = I('post.access');
			$rule = join(',',$access);
			$gid = I('post.gid');
			if(M('AuthGroup')->where(array('id'=>$gid))->setField('rules',$rule)){
				$this->success('权限设置成功');
			}else{
				$this->error('分配权限失败');
			}
		}else{
			$this->error('页面不存在');
		}
	}
	
	

	/**
	 * 节点管理
	 * @return [type] [description]
	 */
	public function rule()
	{
		$this->data = S('menu');
		$this->display();
	}

	/**
	 * 节点添加
	 * @return [type] [description]
	 */
	public function ruleAdd()
	{
		if(IS_POST){
			$model = D('AuthRule');
			if(!$model->create()) $this->error($model->getError());
			if($model->add()){
				// 更新缓存
				$this->ruleCache();
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->pid = I('get.pid',0,'intval');
			$this->menu = S('menu');
			$this->display();
		}
	}
	
	/**
	 * 修改节点
	 * @var string
	 */
	public function ruleEdit()
	{
		$id = I('get.id',0,'intval');
		if($id){
			// 旧数据
			$this->data = M('AuthRule')->find($id);
			$this->menu = S('menu');
			$this->display();
		}else{
			$this->error('页面不存在');
		}
	}

	/**
	 * 节点排序
	 * @return [type] [description]
	 */
	public function ruleSort()
	{
		if(IS_POST){
			$sort = I('post.sort');
			$db = M('AuthRule');
			foreach ($sort as $k => $v) {
				$db->where(array('id'=>$k))->setField('sort',$v);
			}
			$this->ruleCache();
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 修改表单提交
	 * @return [type] [description]
	 */
	public function ruleEditAjax()
	{
		if(IS_POST){
			$model = D('AuthRule');
			if(!$model->create()) $this->error($model->getError());
			if($model->save()){
				$this->ruleCache();
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}else{
			$this->error('页面不存在');
		}
	}

	/**
	 * 删除节点
	 * @return [type] [description]
	 */
	public function delNode()
	{
		if($id = I('post.id',0,'intval')){
			if(M('AuthRule')->delete($id)){
				$this->ruleCache();
				$this->success('删除成功');
				exit;
			}
		}
		$this->error('页面不存在');
	}


	// 节点缓存
	public function ruleCache()
	{
		$data = M('AuthRule')->order('sort')->select();
		$tree = new \Common\Tool\Tree('id');
		$arr = $tree->Tree($data);
		return S('menu',$arr);
	}

}